﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmProductCategoryList : Form
    {
        private bool formLoaded = false;
        public frmProductCategoryList()
        {
            InitializeComponent();

            LoadProductCate(true);
            setButtonEditEnable();
        }

        public void ReloadList()
        {
            LoadProductCate(ckboxHideInactive.Checked);
            setButtonEditEnable();
        }

        private void setButtonEditEnable()
        {
            btnEdit.Enabled = listProductCate.SelectedItems.Count > 0;
        }

        private void LoadProductCate(bool toHideInactive)
        {
            listProductCate.Clear();
            listProductCate.ItemCheck += new ItemCheckEventHandler(list_ItemCheck);
            listProductCate.View = View.Details;
            // Display check boxes.
            listProductCate.CheckBoxes = true;
            // Select the item and subitems when selection is made.
            listProductCate.FullRowSelect = true;
            // Display grid lines.
            listProductCate.GridLines = true;

            coffeeDataSet.ProductCategoryDataTable _list = ProductCategory.LoadCategories(toHideInactive);
            listProductCate.Columns.Add("+", -1, HorizontalAlignment.Left);
            listProductCate.Columns.Add("Tên", 100, HorizontalAlignment.Left);
            listProductCate.Columns.Add("ProductCategoryId", 0, HorizontalAlignment.Left);

            
            foreach (DataRow row in _list.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Checked = (bool)row["IsActive"];
                item.SubItems.Add(row["Name"].ToString());
                item.SubItems.Add(row["ProductCategoryId"].ToString());

                listProductCate.Items.Add(item);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmEditProductCate editForm = new frmEditProductCate(null);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (listProductCate.SelectedItems.Count == 0)
            {
                MessageBox.Show("Hãy chọn một nhóm hàng trước");
                return;
            }
            ListViewItem selectedItem = listProductCate.SelectedItems[0];
            ProductCategory theProductCate = new ProductCategory(int.Parse(selectedItem.SubItems[2].Text));
            theProductCate.Name = selectedItem.SubItems[1].Text;

            frmEditProductCate editForm = new frmEditProductCate(theProductCate);
            editForm.StartPosition = FormStartPosition.CenterParent;
            editForm.MyParentForm = this;
            editForm.ShowDialog(this);
        }

        private void list_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
        {
            if (!formLoaded)
                return;

            bool isActive = e.CurrentValue == CheckState.Unchecked;

            ProductCategory theProductCate = new ProductCategory(int.Parse(listProductCate.Items[e.Index].SubItems[listProductCate.Columns.Count - 1].Text));
            theProductCate.IsActive = isActive;

            if (!theProductCate.UpdateActiveness())
            {
                MessageBox.Show("Có lỗi khi thay đổi kích hoạt của nhóm hàng");
            }
        }

        private void frmProductCategoryList_Shown(object sender, EventArgs e)
        {
            formLoaded = true;
        }

        private void ckboxHideInactive_CheckedChanged(object sender, EventArgs e)
        {
            if (!formLoaded)
                return;

            bool isChecked = ((CheckBox)sender).Checked;

            LoadProductCate(isChecked);
        }

        private void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            setButtonEditEnable();
        }


    }
}
